<?php


namespace app\base\service;

use think\admin\service\AdminService;

use think\Cache;


/**
 * 小甲鱼服务
 * Class AdminService
 * @package think\admin\service
 */
class XjyService extends AdminService
{



    protected $url = 'https://api.yidevs.com/app/human/human/';
    //开发环境
    //private $callbackHost  = 'http://dev.blueant.pro/base/api.Chanjing/';
    //生产环境
    private $callbackHost  = 'http://dev.blueant.pro/base/api.Xiaojiayu/';
    private $Authorization = "803FBF63E15A8137EFBEF18376922390.FF37E0176A7A6FD790F98F2F568F8CAF.GNKYXZWF";
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        if($BACKEND_DOMAIN = env('BACKEND_DOMAIN', '')){
            $this->callbackHost= $BACKEND_DOMAIN.'/base/api.Xiaojiayu/';
        }
       // $callbackHost= env('DB_MYSQL_DATABASE', 'ai_redian_video').'http://dev.blueant.pro/base/api.Chanjing/';
    }

    protected function resulet($code=0,$msg='',$data=[])
    {
        return [
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        ];
    }

    //创建数字人
    public function createAvatar($name,$material_video)
    {
        $retryCount = 0;
        do{

            $url = $this->url."Scene/created";
            $data= [
                'video_name'=>$name,
                'video_url'=>$material_video,
                'callback_url'=>$this->callbackHost."createAvatarBack",
                // 'train_type'=>"figure"
            ];
            $options['headers'] = ['Authorization:Bearer '.$this->Authorization,'Content-Type: application/json'];
            $options['timeout'] = 120;
            $res = http_post($url,json_encode($data),$options);
            $res = json_decode($res,true);
            $this->app->log->error("createAvatarServiceXjy:".json_encode($res));
            if($res){
                if($res['code']==200){
                    return $this->resulet(1,'success',$res['data']['scene_task_id']);
                }elseif($res['code']==404){
                    //QPS超限等待60秒
                    sleep(60);
                    $retryCount++;
                }else{
                    return $this->resulet(0,$res['msg']);
                }
            }else{
                return $this->resulet(0,'请求失败');
                //QPS超限等待60秒
                sleep(60);
                $retryCount++;

            }
        }while($retryCount < 10);
        return $this->resulet(0,'请求失败');
    }


    //生成口播视频
    public function createAvatarVideo($avatar_id,$voice_url)
    {
        $retryCount = 0;
        do{

            $url = $this->url."Musetalk/create";
            $data = [
                "scene_task_id"=>$avatar_id,
                'audio_url'=>$voice_url,
                'callback'=>"",
            ];
            $options['headers'] = ['Authorization:Bearer '.$this->Authorization,'Content-Type: application/json'];
            $options['timeout'] = 600;
            $res = http_post($url,json_encode($data),$options);
            $res = json_decode($res,true);

            if($res){
                if($res['code']==200){
                    return $this->resulet(1,'success',$res['data']['video_task_id']);
                }elseif($res['code']==404){
                    //QPS超限等待60秒
                    $this->app->log->error("QPS超限等待60秒");
                    sleep(60);
                    $retryCount++;
                }else{
                    return $this->resulet(0,$res['msg']);
                }
            }else{
                return $this->resulet(0,'请求失败');
            }
        }while($retryCount < 10);



    }

    //查询视频生成状态
    public function getAvatarVideoInfo($task_id)
    {
        $retryCount = 0;
        do{
            $url = $this->url."Musetalk/task?task_id={$task_id}";

            $options['headers'] = ['Authorization:Bearer '.$this->Authorization];
            $res = http_get($url,[],$options);
            $res = json_decode($res,true);
            $this->app->log->error("小甲鱼getAvatarVideoInfo:".json_encode($res));
            if($res){
                if($res['code']==200){
                    return $this->resulet(1,'success',$res['data']);
                }elseif($res['code']==404){
                    //QPS超限等待60秒
                    $this->app->log->error("QPS超限等待60秒");
                    sleep(10);
                    $retryCount++;
                }else{
                    $this->app->log->error("小甲鱼getAvatarVideoInfo:".json_encode($res));
                    return $this->resulet(0,$res['msg']);
                }
            }else{
                return $this->resulet(0,'请求失败');
            }
        }while($retryCount < 10);

    }




}
